package com.example;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.database.CurriculumMessage;
import com.example.database.MyCourseMessage;
import com.example.entity.Curriculum;
import com.example.entity.MyCourse;
import com.example.view.Customize;

import java.util.ArrayList;
import java.util.List;

public class ChooseCurriculum extends AppCompatActivity {
    private Customize customize ;
    private ListView listView ;
    public ArrayList<String> chooseCurriculum = new ArrayList<>();
    private ArrayAdapter<Curriculum> adapter ;
    private CurriculumMessage curriculumMessage ;

    @SuppressLint("MissingInflatedId")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_curriculum);
        customize = findViewById(R.id.curriculum_top) ;
        listView = findViewById(R.id.list_view);

        curriculumMessage = new CurriculumMessage(this);
        MyCourseMessage myCourseMessage = new MyCourseMessage(this);
        List<Curriculum> curriculums = curriculumMessage.getAllCurriculum();
        adapter = new ArrayAdapter<com.example.entity.Curriculum>(this , R.layout.choosecurriculum,R.id.showName , curriculums) {
            public View getView(int position , View converView , ViewGroup parent){
                View view = super.getView(position , converView , parent);
                TextView textName = view.findViewById(R.id.showName);
                TextView textId = view.findViewById(R.id.showId);
                ImageButton add = view.findViewById(R.id.add);
                ImageButton remove = view.findViewById(R.id.remove);
                Curriculum curriculum = getItem(position) ;
                if (curriculum != null){
                    textName.setText(String.valueOf(curriculum.getCurriculumName()));
                    textId.setText(String.valueOf(curriculum.getCurriculumId()));
                    // 为textName添加点击事件
                    textName.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TextView getId = view.findViewById(R.id.showId);
                            Log.d("", "+++++++++++++++++++++"+getId+"++++++++++++++++");
                            String curriculumId = getId.getText().toString() ;
                            Curriculum curriculum = curriculumMessage.getCourseDetails(curriculumId);
                            showCurriculumDetailsDialog(curriculum);
                        }
                    });

                    // 为textId添加点击事件
                    textId.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TextView getId = view.findViewById(R.id.showId);
                            Log.d("", "+++++++++++++++++++++"+getId+"++++++++++++++++");
                            String curriculumId = getId.getText().toString() ;
                            Curriculum curriculum = curriculumMessage.getCourseDetails(curriculumId);
                            showCurriculumDetailsDialog(curriculum);
                        }
                    });
                }

                //拿到登录界面的用户Id
                SharedPreferences sharedPreferences = getSharedPreferences("loginIdAndName" , Context.MODE_PRIVATE);
                String studentId = sharedPreferences.getString("studentId", "");

                add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView getCId = view.findViewById(R.id.showId);
                        String curriculumId = getCId.getText().toString() ;
//                        Toast.makeText(ChooseCurriculum.this, studentId+curriculumId, Toast.LENGTH_SHORT).show();

                        new AlertDialog.Builder(ChooseCurriculum.this)
                                .setMessage("确定选择该课程吗？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        boolean isChoose = myCourseMessage.isCourseChosen(studentId ,curriculumId);
                                        // 用户点击确定，执行退出登录操作
                                        if (!isChoose){
                                            myCourseMessage.addChoose(studentId , curriculumId);
                                            Toast.makeText(ChooseCurriculum.this, "课程添加成功！", Toast.LENGTH_SHORT).show();
                                        }
                                        else {
                                            Toast.makeText(ChooseCurriculum.this, "该课程已经添加", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                })
                                .setNegativeButton("取消", null) // 点击取消，对话框消失，不执行任何操作
                                .show();
                    }
                });
                remove.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView getCId = view.findViewById(R.id.showId);
                        String curriculumId = getCId.getText().toString() ;
                        new AlertDialog.Builder(ChooseCurriculum.this)
                                .setMessage("确定移除该课程吗？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        boolean isDel = myCourseMessage.deleteChoose(studentId , curriculumId);
                                        // 用户点击确定，执行退出登录操作
                                        if (isDel){
                                            Toast.makeText(ChooseCurriculum.this, "该课程已经移除课程", Toast.LENGTH_SHORT).show();
                                        }else {
                                            Toast.makeText(ChooseCurriculum.this, "您未选择该课程", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                })
                                .setNegativeButton("取消", null) // 点击取消，对话框消失，不执行任何操作
                                .show();
                    }
                });
                return view ;
            }
        };

        listView.setAdapter(adapter);
        customize.setRightBtnNoVisible(true);
        customize.setTitleText("选课");
        customize.setLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void showCurriculumDetailsDialog(Curriculum curriculum) {
        if (curriculum != null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ChooseCurriculum.this);
            builder.setTitle("课程信息");
            builder.setMessage("课程ID: " + curriculum.getCurriculumName() + "\n"
                    + "课程名称: " + curriculum.getCurriculumId() + "\n"
                    + "授课教师: " + curriculum.getCurriculumTeacher() + "\n"
                    + "上课班级: " + curriculum.getCurriculumClass() + "\n"
                    + "上课时间: " + curriculum.getCurriculumTime()+"学时");
            builder.setPositiveButton("确定", null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }}
}
